import pandas as pd
from Queen.config.mission_config import main_mission_dict
from Queen.kafka.queen_producer import produce_to_slave
from logger_config import logger_factory

logger = logger_factory()


def query_file(path: str):
    try:
        data = pd.read_csv(path, sep=',')
        logger.info(f'read csv file {path}')
    except Exception as e:
        logger.error(f'read csv file {path} fail, please check it')
        return None
    return data


def dispatch_mission(mission: str, mission_dict: dict):
    """
    :param mission: 任务名
    :param mission_dict: 任务字典
    :return:
    """
    datasource = mission_dict[mission]
    data = datasource.read_data()
    if data is None or data.empty:
        logger.info(f'mission {mission} file data is empty, skip dispatch it')
        return
    if mission in main_mission_dict:
        queue_name = 'main'

    produce_to_slave(mission, data, queue_name)
    logger.info(f'Dispatch mission {mission}')
